// pages/me/me.js
import wxRequest from "../../utils/wxRequest"
const app = getApp();
Page({  
  /**
   * 页面的初始数据
   */
  data: {
    imagesUrl:app.globalData.imagesUrl,
    currentTabs: 0,
    winWidth: 0,
    winHeight: 0,
    rmkclist:"",
    stList: "",
    cateList:"",
    articleList:"",
    teacherList:"",
    staticUrl:"https://www.sdwxedu.com/static/",
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var openid=wx.getStorageSync('openid');
    
    if(openid){
      this.initData(0)
    }else{
      var that=this;
      wx.login({
        success: res => {
          // 发送 res.code 到后台换取 openId, sessionKey, unionId
          wx.request({
            url: 'https://www.sdwxedu.com/applet/auth/wxLogin',
            data:{code:res.code},
            success:function(result){
              wx.setStorageSync('openid', result.data.openid);
              that.initData(0);
            }
          })
        }
      })
    }
  },
  initData: function (cate) {
    var that = this;
    var option={url:"index/list",data:{cate:cate}};
    wxRequest(option).then(function(res){
        var lists=res.data.data;
        that.setData({
          rmkclist: lists,
        })
        that.setHeight();
    });
    var that=this;
      var option={url:"index/initData",data:""};
      wxRequest(option).then(function(res){
          var datas=res.data.data;
          console.log(datas);
          that.setData({
            stList: datas.stList,
            cateList:datas.courseCate,
            teacherList:datas.teacher,
            articleList:datas.article
          })
      });
  },

  // 设置热门课程tab内容自适应高度
  setHeight: function () {
    var query = wx.createSelectorQuery();
    var that = this;
    query.select('#v1').boundingClientRect(function (rect) {
      console.log(rect.height + 30);
      that.setData({
        winHeight: rect.height + 30
      })
    }).exec();
  },

  // 设置热门课程tab内容切换
  swicthNav: function (e) {
    var page = this;
    this.initData(e.target.dataset.current);
    this.setHeight();
    if (this.data.currentTabs == e.target.dataset.current) {
      return false;
    } else {
      page.setData({ currentTabs: e.target.dataset.current })
    }
  },
  toCourse:function(){
    // var cate=this.data.currentTabs
    // wx.setStorage({key:"cate",data:cate})
    wx.switchTab({
      url: '../course/course',
    })
  },
  showDetail:function(e){
    var cid=e.currentTarget.dataset.cid;
    wx.navigateTo({
      url: '../courseArticle/courseArticle?cid='+cid,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
      this.onLoad();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})